// pages/beebox/edit/edit.js
import http from '../../../http/http.js'
import utils from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pars:{
      deviceCode:'',
      name:'',
      apiaryName:'',
      apiaryId:'',
      honeycomb:'',
      queenBeeGeneration:'',
      talkOfficeTime:'',
      beeTypeName:'',
      id: '',
    },
    talkOfficeTimestr:'',
    ac_name: false,
    ac_honeycomb: false,
    queenBeeGeneration: ['第一代', '第二代', '第三代', '第四代', '第五代', '第六代', '第七代', '第八代', '第九代', '第十代', '第十一代', '第十二代', '第十三代']

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getbeeboxinfo(options.deviceCode)
    this.data.pars.id=options.id
  },
  getbeeboxinfo: function (arg) {
    
    http.getdata('/api/beehive/get?deviceCode=' + arg).then((res) => {
      this.setData({
        ['pars.deviceCode']: res.data.result.deviceCode,
        ['pars.name']: res.data.result.name,
        ['pars.apiaryName']: res.data.result.apiaryName,
        ['pars.apiaryId']: res.data.result.apiaryId,
        ['pars.honeycomb']: res.data.result.honeycomb,
        ['pars.beeTypeName']: res.data.result.beeTypeName,
        ['pars.queenBeeGeneration']: res.data.result.queenBeeGeneration,
        ['pars.talkOfficeTime']: res.data.result.talkOfficeTime,
        talkOfficeTimestr: res.data.result.talkOfficeTime ? utils.formatTimeTwo(res.data.result.talkOfficeTime, 'Y-M-D'):'',
      })
    })
  },
  bindPickerChange:function(e){
    let index = e.detail.value
    this.setData({
      ['pars.queenBeeGeneration']: this.data.queenBeeGeneration[index]
    })
  },
  bindPickerChangetime:function(e){
    console.log()
    this.setData({
      talkOfficeTimestr: e.detail.value,
      ['pars.talkOfficeTime']: new Date(e.detail.value).getTime()
    })
  },
  getvalue:function(e){
    console.log(e)
    let type = e.currentTarget.dataset.type
    if (type == "name"){
      this.setData({
        ['pars.name']: e.detail.value
      })
    }else{
      this.setData({
        ['pars.honeycomb']: e.detail.value
      })
    }
  },
  active: function (e) {
    let type = e.currentTarget.dataset.type
    let val = e.detail.value
    if (type == "name") {
      this.setData({
        ac_name: true,
        ac_honeycomb: false,
      })
    } else {
      this.setData({
        ac_name: false,
        ac_honeycomb: true,
      })
    }
  },
  save:function(){
    http.putdata('/api/beehive/edit',this.data.pars).then((res)=>{
      wx.showToast({
        title: '修改成功',
        duration:2000,
        success:function(){
          wx.navigateBack()
        }
      })
    })
  }
})